package cn.tedu.em.car.webapi.service.impl;

import cn.tedu.em.car.service.ICarService;
import cn.tedu.em.car.webapi.mapper.CarMapper;
import cn.tedu.em.common.exception.EstateManagementServiceException;
import cn.tedu.em.common.pojo.car.dto.CarAddNewDTO;
import cn.tedu.em.common.pojo.car.dto.CarUpdateDTO;
import cn.tedu.em.common.pojo.car.entity.Car;
import cn.tedu.em.common.pojo.car.vo.CarListForUserVO;
import cn.tedu.em.common.pojo.car.vo.CarListVO;
import cn.tedu.em.common.pojo.car.vo.CarStandardVO;
import cn.tedu.em.common.restful.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CarService implements ICarService {

    @Autowired
    private CarMapper carMapper;

    @Override
    public void addNew(CarAddNewDTO carAddNewDTO) {
        log.debug("开始处理【添加车辆】的业务,参数:{}", carAddNewDTO);
        String carNum = carAddNewDTO.getCarNum();
        int count = carMapper.countByCarNum(carNum);
        if (count > 0){
            String message = "添加车辆失败,车牌号已存在!";
            log.warn(message);
            throw new EstateManagementServiceException(
                    ResponseCode.CONFLICT, message);
        }
        Car car = new Car();
        BeanUtils.copyProperties(carAddNewDTO, car);
        int rows = carMapper.insertCar(car);
        if (rows != 1){
            String message = "新增车辆失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new EstateManagementServiceException(
                    ResponseCode.INTERNAL_SERVER_ERROR, message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【删除车辆】的业务,参数:{}", id);
        CarStandardVO carInfo = carMapper.getCarInfoById(id);
        if (carInfo == null){
            String message = "删除车辆失败,尝试删除的车辆数据不存在!";
            log.warn(message);
            throw new EstateManagementServiceException(
                    ResponseCode.NOT_FOUND, message);
        }
        int rows = carMapper.deleteById(id);
        if (rows != 1){
            String message = "删除车辆失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new EstateManagementServiceException(
                    ResponseCode.INTERNAL_SERVER_ERROR, message);
        }
    }

    @Override
    public void deleteByUserId(Long userId) {
        log.debug("开始处理【根据用户id删除车辆】的业务,参数:{}", userId);
        List<CarListForUserVO> list = carMapper.getCarListByUserId(userId);
        if (list.isEmpty()){
            String message = "删除车辆失败,尝试删除的车辆数据不存在!";
            log.warn(message);
            throw new EstateManagementServiceException(
                    ResponseCode.NOT_FOUND, message);
        }
        int rows = carMapper.deleteByUserId(userId);
        if (rows < 1){
            String message = "删除车辆失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new EstateManagementServiceException(
                    ResponseCode.INTERNAL_SERVER_ERROR, message);
        }
    }

    @Override
    public void updateCarInfo(CarUpdateDTO carUpdateDTO) {
        log.debug("开始处理【修改车辆信息】的业务,参数:{}", carUpdateDTO);
        int count = carMapper.countByCarNumAndNotId(
                carUpdateDTO.getId(), carUpdateDTO.getCarNum());
        if (count > 0){
            String message = "修改车辆失败,车牌号已被占用!";
            log.warn(message);
            throw new EstateManagementServiceException(
                    ResponseCode.CONFLICT, message);
        }
        count = carMapper.countByLocationIdAndNotId(
                carUpdateDTO.getId(), carUpdateDTO.getLocationId());
        if (count > 0){
            String message = "修改车辆失败,车位已被占用!";
            log.warn(message);
            throw new EstateManagementServiceException(
                    ResponseCode.CONFLICT, message);
        }
        log.debug("dto:{}",carUpdateDTO);
        Car car = new Car();
        log.debug("car:{}",car);
        BeanUtils.copyProperties(carUpdateDTO, car);
        int rows = carMapper.updateById(car);
        if (rows != 1){
            String message = "修改车辆失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new EstateManagementServiceException(
                    ResponseCode.INTERNAL_SERVER_ERROR, message);
        }
    }

    @Override
    public CarStandardVO getCarInfoById(Long id) {
        log.debug("开始处理【根据id查询车辆详情】的业务,参数:{}", id);
        CarStandardVO carInfo = carMapper.getCarInfoById(id);
        if (carInfo == null){
            String message = "查询车辆详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new EstateManagementServiceException(
                    ResponseCode.INTERNAL_SERVER_ERROR, message);
        }
        return carInfo;
    }

    @Override
    public List<CarListVO> getCarList() {
        log.debug("开始处理【查询车辆列表】的业务");
        List<CarListVO> carList = carMapper.getCarList();
        return carList;
    }

    @Override
    public List<CarListForUserVO> getCarListByUserId(Long userId) {
        log.debug("开始处理【根据用户id查询车辆列表】的业务,参数:{}", userId);
        List<CarListForUserVO> carList = carMapper.getCarListByUserId(userId);
        return carList;
    }
}
